package com.newbie.pet.service.impl;

import com.newbie.pet.dao.CustomerRepository;
import com.newbie.pet.model.CustomerEntity;
import com.newbie.pet.wrap.DPage;
import com.newbie.pet.wrap.P;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

/**
 * 类描述：顾客信息
 *
 * @author zhangkailiang
 * @date 2020/5/16
 */
@Service
public class CustomerService {

    @Autowired
    private CustomerRepository customerRepository;

    public long save(CustomerEntity customerEntity) {
        return customerRepository.save(customerEntity).getId();
    }

    public CustomerEntity findOneById(Long id) {
        return customerRepository.findById(id).get();
    }

    public Boolean deleteById(Long id) {
        customerRepository.deleteById(id);
        return true;
    }

    public DPage<CustomerEntity> findPageByCustomer(P<CustomerEntity> p) {
        Page<CustomerEntity> page;
        if (p.getParam() == null) {
            page = customerRepository.findAll(p.getPage().toPage());
        } else {
            page = customerRepository.findAll(Example.of(p.getParam()), p.getPage().toPage());
        }

        return DPage.of(p.getPage(), page);
    }

    public Boolean updateById(CustomerEntity customerEntity) {
        customerRepository.save(customerEntity);
        return true;
    }
}
